<?php
class IndexController extends Yaf_Controller_Abstract {

   public function init(){
         $news_menu = $about_us_article = array();
         $product_model = new productModel();
         $product_list  = $product_model->select(array('status'=>'1'),array('order'=>1)); 

         $type_model    = new typeModel();
         $article_model = new articleModel();

         $newstype      = $type_model->select(array('name'=>'新闻动态'));
         if(count($newstype) != 0){
            $newstype_id   = $newstype[0]['_id'];
            $news_menu     = $type_model->select(array('fid'=>(string)$newstype_id,'status'=>"1"));
         }

         $about_us      = $type_model->select(array('name'=>'关于惠信'));
         if(count($about_us) != 0){
            $about_us_id   = $about_us[0]['_id'];
            $about_us_article = $article_model->select(array('status'=>'1','typeid'=>(string)$about_us_id));
         }
         

         $data = array(
               'product_list' => $product_list,
               'news_menu'    => $news_menu,
               'about_us'     => $about_us_article,
            );
         $this->getView()->assign($data);  
   }

   public function indexAction() {//默认Action
         $type_model    = new typeModel();
         $article_model = new articleModel();
         $ad_model      = new adModel();
         $newstype      = $type_model->select(array('name'=>'新闻动态'));
         
         $article_list = array();

         if(count($newstype) != 0){
            $newstype_id   = (string)$newstype[0]['_id'];
            $newstype_id_list = $type_model->select(array('status'=>"1",'fid'=>$newstype_id));
            foreach ($newstype_id_list as $key => $value) {
               $id_list[] = (string)$value['_id'];
            }

            $article_list = $article_model->select(array('status'=>'1','typeid'=>array('$in'=>$id_list)),array('addtime'=>1),4);
         }

         //middle ad
         $ad_list = $ad_model->select(array('status'=>'1'));
         $data = array(
               'title' => '惠信',
               'index_article_list'=> $article_list,
               'ad_list' => $ad_list
         );
       	$this->getView()->assign($data);
   }
   /**
    * 这个要与路由配合
    * @return [type] [description]
    */
   public function newsAction(){
      //id表示文章id  _id表示分类id
      $get           = $this->getRequest()->getParams();
      $article_model = new articleModel();
      $type_model    = new typeModel();
      if(empty($get)){
         $article_list = array();
         $newstype      = $type_model->select(array('name'=>'新闻动态'));
         if(count($newstype) != 0){
            $newstype_id   = (string)$newstype[0]['_id'];
            $newstype_id_list = $type_model->select(array('status'=>"1",'fid'=>$newstype_id));
            foreach ($newstype_id_list as $key => $value) {
               $id_list[] = (string)$value['_id'];
            }

            $article_list = $article_model->select(array('status'=>'1','typeid'=>array('$in'=>$id_list)));
         }
         $data = array(
            'FLAG'         => true,
            'title'        => '惠信文章页',
            'content_title'=> '新闻动态',
            'article_list' => $article_list
         );

      }else{

         $type_info = $type_model->select(array('_id'=>new MongoId($get['_id'])));

         if($get['id'] === "0" && $get['_id'] != ''){
            //显示分类
            $article_list = $article_model->select(array('status'=>'1' , 'typeid'=>$get['_id']));
            $data = array(
               'FLAG' => true,
               'title'=>'惠信文章页',
               'article_list'=>$article_list,
            );
         }else{
            //详情
            $article_detail = $article_model->select(array('status'=>'1' , '_id'=> new MongoId($get['id'])));
            $data = array(
               'FLAG' => false,
               'title'=>'惠信文章页',
               'article_detail'=> isset($article_detail[0]) ? $article_detail[0]:''
               );
         }

         $data['active_type_id'] = $get['_id'];
         $data['content_title'] = isset($type_info[0]) ? $type_info[0]['name']:'新闻动态';
      }
      $this->getView()->assign($data);
   }

   public function aboutusAction(){
      $get           = $this->getRequest()->getParams();
      $type_model    = new typeModel();
      $article_model = new articleModel();
      $active_article = array();

      if(empty($get)){

         $about_us         = $type_model->select(array('name'=>'关于惠信'));
         if(count($about_us) != 0){
            $about_us_id      = $about_us[0]['_id'];
            $about_us_article = $article_model->select(array('status'=>'1','typeid'=>(string)$about_us_id));
            
            $article_detail[] = $about_us_article[0]; 
            $active_article   = $about_us_article[0]['_id'];
         }
      }else{

         $article_detail   = $article_model->select(array('status'=>'1' , '_id'=> new MongoId($get['about_id'])));
         $active_article   = $get['about_id'];

      }
      
      $data = array(
            'title'           => '关于惠信',
            'article_detail'  => isset($article_detail[0]) ? $article_detail[0] : 0,
            'active_article'  => $active_article
      );
      $this->getView()->assign($data);
   }


   public function productAction(){
      $this->getView()->assign('title','product');
   }
}
?>
